/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import preparedstatement.DebugLevel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import preparedstatement.StatementFactory;

/**
 *
 * @author Tesla
 */
public class bazar extends javax.swing.JDialog {
Connection cn;
DebugLevel debug = DebugLevel.ON;
PreparedStatement ps;
 ResultSet rs;
 static int usuario;
 static String mas;
 static String gasto;
 
    /**
     * Creates new form bazar
     */
    public bazar(java.awt.Frame parent, boolean modal,int b,String g, String m) {
        super(parent, modal);
        initComponents();
        usuario=b;
        if(m.equals("-")){
            jLabel1.setText("Gastos");
            jLabel2.setText("Cantidad:");
        }
        mas=m;
        gasto=g;
        jTextField1.setDocument (new LimitadorCaracteresFloat());
        getRootPane().setDefaultButton(jButton2);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Saldos");

        jLabel2.setText("Precio:");

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Aceptar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1))
                    .addComponent(jLabel1))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
  if(!jTextField1.getText().equals(""))
        try {
            String codigo="",cadena;
            if(mas!=null)
            cadena=mas+jTextField1.getText();
            else cadena=jTextField1.getText();
            local_connection conexion;
            conexion = new local_connection();
            cn = conexion.getconnect();
            ps = StatementFactory.getStatement(local_connection.cn,"insert into venta(id_usuario,sucursal,caja) values(?,?,?)",debug );
            ps.setInt(1,usuario);
            ps.setInt(2,VentanaPrincipal.sucursal);
            ps.setInt(3,VentanaPrincipal.caja);
           // System.out.println("insert into venta_principal values(null,null,null,1,CURRENT_DATE,null,null,"+cadena+",null,null,null,0,"+usuario+")");
            
            ps.execute();
           VentanaPrincipal.guardarCache(ps, "venta", true);
            String last=(VentanaPrincipal.local)?"last_insert_rowid":"LAST_INSERT_ID";
            ps = StatementFactory.getStatement(local_connection.cn,"insert into detalleventa(preciovendido,folio,id_producto,cantidad)  values(?,"+last+"(),-3,1)",debug);
            ps.setString(1, cadena);
            ps.execute();
            VentanaPrincipal.guardarCache(ps, "detalleventa", true);
            // TODO add your handling code here:
            this.dispose();
        } catch (SQLException ex) {
            VentanaPrincipal.logger.log(Level.SEVERE, null, ex);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

          class LimitadorCaracteresFloat extends PlainDocument
{
   /**
    * Método al que llama el editor cada vez que se intenta insertar caracteres.
    * Sólo debemos verificar arg1, que es la cadena que se quiere insertar en el JTextField
    */
   public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException
   {
       for (int i=0;i<arg1.length();i++)
          // si no es digit, volvemos
          if (!(Character.isDigit(arg1.charAt(i))||arg1.charAt(i)=='.'))
             return;

       // Si todos son digit, insertamos el texto en el JTextField
       super.insertString(arg0, arg1, arg2);
   }
} 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
